<?php

class searcharea{
public function index(){
$city_id = intval($GLOBALS['request']['city_id']);
$trad_id = intval($GLOBALS['request']['trad_id']);
$cate_id= intval($GLOBALS['request']['cate_id']);
$cate_type_id = intval($GLOBALS['request']['cate_type_id']);
$ordertype=$GLOBALS['request']['order_by_type'];
$page = intval($GLOBALS['request']['page']);
$page=$page==0?1:$page;
$page_size = PAGE_SIZE;
$limit = (($page-1)*$page_size).','.$page_size;
$distance = intval($GLOBALS['request']['distance']);
$lat = trim($GLOBALS['request']['log']);
$log =trim($GLOBALS['request']['lat']);
$area_name=$GLOBALS['db']->getOne('select name from '.DB_PREFIX."area where id='{$trad_id}' ");
$area_unicode = str_to_unicode_string($area_name);
$order_type=get_order_type($ordertype);
if(empty($cate_type_id ) or $cate_type_id=='null'){
$sql_count = 'SELECT COUNT(id)  FROM '.DB_PREFIX."supplier_location  WHERE match locate_match against('".$area_unicode."' IN BOOLEAN MODE) AND deal_type=0 and  city_id='{$city_id}'    ";
$count=$GLOBALS['db']->getOne($sql_count);
$sql= 'SELECT id,name,address,preview,avg_point,xpoint,ypoint,avg_price as price_type  FROM '.DB_PREFIX."supplier_location    WHERE match locate_match against('".$area_unicode."' IN BOOLEAN MODE) AND deal_type=0 and   city_id='{$city_id}'   order by {$order_type} desc limit ".$limit;
}else
{
$sql_count = 'SELECT COUNT(sl.id)  FROM '.DB_PREFIX.'supplier_location as sl left join '.DB_PREFIX."deal_cate_type_location_link as ct on sl.id=ct.location_id  WHERE match sl.locate_match against('".$area_unicode."' IN BOOLEAN MODE) AND deal_type=0 and ct.deal_cate_type_id='{$cate_type_id}' and sl.city_id='{$city_id}'   ";
$count=$GLOBALS['db']->getOne($sql_count);
$sql= 'SELECT sl.id,sl.name,sl.address,sl.preview,sl.avg_point,sl.xpoint,sl.ypoint,sl.avg_price as price_type  FROM '.DB_PREFIX.'supplier_location as sl  left join  '.DB_PREFIX."deal_cate_type_location_link as dctl on sl.id=dctl.location_id  WHERE match sl.locate_match against('".$area_unicode."' IN BOOLEAN MODE) AND sl.deal_type=0 and  dctl.deal_cate_type_id='{$cate_type_id}' and sl.city_id='{$city_id}'  order by sl.{$order_type} desc  limit ".$limit;
}
$list= $GLOBALS['db']->getAll($sql);
$root['menu']=  $GLOBALS['db']->getAll('SELECT id as trad_id,city_id,name FROM '.DB_PREFIX."area WHERE pid=0 and city_id='{$city_id}' ");
foreach($list as $k=>$v)
{
$list[$k]['point']=score2Half($v['avg_point']);
$list[$k]['distance']=GetDistance($lat,$log,$v['xpoint'],$v['ypoint']);
$list[$k]['image']=get_abs_img_root(get_spec_image($v['preview'],640,240,1));
$dish=$GLOBALS['db']->getAll('SELECT ct.name   FROM '.DB_PREFIX.'deal_cate_type_location_link as ctl left join '.DB_PREFIX."deal_cate_type as ct on ctl.deal_cate_type_id=ct.id  WHERE  ctl.location_id='{$v['id']}' ");
$dish_array=array();
foreach($dish as $dk=>$dv)
{
array_push($dish_array,$dv['name']);
}
$list[$k]['specialty_dish']= implode('  ',$dish_array);
$list[$k]['avg_price_format'] = '￥'.$GLOBALS['lang']['AVG_PRICE_'.$v['price_type']];
if(empty($list[$k]['specialty_dish']))
{
unset($list[$k]['specialty_dish']);
}
unset($list[$k]['preview'],$list[$k]['xpoint'],$list[$k]['avg_point'],$list[$k]['ypoint'],$list[$k]['price_type']);
}
$page_total = ceil($count/$page_size);
$root['item']=$list;
$root['page'] = array('page'=>$page,'page_total'=>$page_total);
output($root);
}
}
?>